<?php
class Admin_BookCategoryController extends Zend_Controller_Action
{
    public function init()
    {}
    public function indexAction()
    {
        $book=new Admin_Model_MBook();
        $this->view->cats=$book->getAllCategories(array("_id","name"));
        $request=$this->getRequest();
        $this->view->work=$request->getParam('work', $default='addnew');// lấy từ tham số trên url
        $this->view->idCat=$request->getParam('idCat', $default='0');   // lấy từ tham số trên url
        $this->view->catName=$book->getCategory($this->view->idCat);
        
        //echo 'id_cat:'.$request->getParam('id_cat').'<br/>';
        //echo 'work:'.$request->getParam('work').'<br/>';
        //echo 'idCat:'.$request->getParam('idCat').'<br/>';
        //echo '_action:'.$request->getParam('_action').'<br/>';
        //echo 'name_cat:'.$request->getParam('name_cat').'<br/>';
        
        //delete category
        if($this->view->work=='delete' && $this->view->idCat!='0')
        {
            $book->deleteCategory($this->view->idCat);
        }
        //add category
        if($request->getParam('_action')=='addnew') // lấy từ input hidden
        {
            $newCatName=$request->getParam('name_cat'); // lấy từ input text
            $book->addCategory($newCatName);
        }
        // edit category
        if($this->view->idCat =='0' && $request->getParam('_action')=='edit')
        {
            $id=new MongoId($request->getParam('id_cat'));  // lấy từ input hidden
            $newCatName=$request->getParam('name_cat');     // lấy từ input text
            $book->changeCategory($newCatName, $id);
        }
        
        $this->_helper->layout()->setLayout('layout_admin');
    }
}
?>
